<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller {
	
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://localhost/ci/index.php/product
	 *	- or -  
	 * 		http://localhost/ci/index.php/product/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://localhost/ci/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/product/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index($offset = 0)
	{
		$limit = 10;
		
		$this->load->model('order_model');
		
		$orders = $this->order_model->getOrders($limit, $offset);
		
		$data['orders'] = $orders['rows'];
		$data['totalRows'] = $orders['totalRows'];
                
                foreach($orders['rows'] as $orderKey => $order ) {
                    $orderDetails = $this->order_model->getOrderDetails($order->customer_code,$order->date_bought);
                    $data['orderDetails'][$orderKey] = $orderDetails;
                    $data['userDetail'][$orderKey] = $this->order_model->getUser($order->customer_code);
                    
                    foreach($orderDetails as $orderDetailKey => $orderDetail ) {
                        $productCategory = explode(',', $orderDetail->category);
                        $productTable = ($productCategory[1] != "") ? $productCategory[1] : $productCategory[0];
                        $data['productDetail'][$orderKey] = $this->order_model->getProduct($productTable, $orderDetail->id_product);
                    }
                }

		// pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('/order/index');
		$config['total_rows'] = $data['totalRows'];
		$config['per_page'] = $limit;
		$config['url_segment'] = 3;
		$config['first_link'] = 'First';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$this->load->view('order', $data);
	}
        
        public function update_status() {
            if($_SERVER['REQUEST_METHOD'] == 'POST'){
                
                $this->load->model('order_model');
                $id = $this->input->post('pID');
                //$checkbox = $this->input->post('selectedBox');
                $status = $this->input->post('pStatus');
                $this->order_model->updateStatusByID($id,$status);
            }
            $this->index();
        }
}

/* End of file product.php */
/* Location: ./application/controllers/product.php */